package com.qdy.service.impl;

import com.qdy.repository.CourseRepository;
import com.qdy.repository.TeamMemberRepository;
import com.qdy.repository.TeamRepository;
import com.qdy.service.TeamService;
import com.qdy.dto.TeamDto;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * Created by qudingyi on 2017/4/22.
 */
@Service
public class TeamServiceImpl implements TeamService{

    @Autowired
    private TeamMemberRepository teamMemberRepository;



    @PersistenceUnit
    private EntityManagerFactory emf;

    @Override
    public TeamDto getTeamInfo(Long teamId) {
        TeamDto view=new TeamDto();

        return view;
    }

    @Override
    public List<Map<String,Object>> findList(Long userId) {
        EntityManager em=emf.createEntityManager();
        StringBuffer buffer=new StringBuffer("select");
        buffer.append(" tm.id as teamId,");
        buffer.append(" tm.address,");
        buffer.append(" tm.type,");
        buffer.append(" tm.depict,");
        buffer.append(" tm.geohash,");
        buffer.append(" tm.team_icon as teamIcon,");
        buffer.append(" tm.team_name as teamName,");
        buffer.append(" COUNT(tmm.id) as enrollNumber,");
        buffer.append(" CASE WHEN COUNT(tmmm.id)>0 THEN true ELSE false end  as isEnroll");
        buffer.append(" from t_team  tm");
        buffer.append(" LEFT JOIN t_team_member tmm on tmm.team_id=tm.id");
        buffer.append(" LEFT JOIN t_team_member tmmm on tmmm.team_id=tm.id and tmmm.user_id=:userId");
        buffer.append(" GROUP BY tm.id ORDER BY tm.create_time desc");
        Query nativeQuery = em.createNativeQuery(buffer.toString());
        nativeQuery.setParameter("userId",userId==null?0:userId);
        nativeQuery.unwrap(org.hibernate.SQLQuery.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map<String,Object>> datas = (List<Map<String,Object>>) nativeQuery.getResultList();
        em.close();
        return datas;

    }

    @Override
    public Map<String, Object> findByTeamId(Long temaId,Long userId) {
        EntityManager em=emf.createEntityManager();
        StringBuffer buffer=new StringBuffer("select");
        buffer.append(" tm.id as teamId,");
        buffer.append(" tm.address,");
        buffer.append(" tm.type,");
        buffer.append(" tm.depict,");
        buffer.append(" tm.geohash,");
        buffer.append(" tm.team_icon as teamIcon,");
        buffer.append(" DATE_FORMAT(tm.create_time,'%Y-%m-%d') as createTime,");
        buffer.append(" tm.team_name as teamName,");
        buffer.append(" COUNT(tmm.id) as enrollNumber,");
        buffer.append(" CASE WHEN COUNT(tmmm.id)>0 THEN true ELSE false end  as isEnroll,");
        buffer.append(" CASE WHEN m.identity<>3 THEN true else false end as isMamage, ");
        buffer.append(" m.status as applyStatus");
        buffer.append(" from t_team  tm");
        buffer.append(" LEFT JOIN t_team_member tmm on tmm.team_id=tm.id");
        buffer.append(" LEFT JOIN t_team_member m  on  m.user_id=:mUserId and m.team_id=tm.id");
        buffer.append(" LEFT JOIN t_team_member tmmm on tmmm.team_id=tm.id and tmmm.user_id=:userId");
        buffer.append(" where tm.id=:teamId");
        buffer.append(" GROUP BY tm.id ORDER BY tm.create_time desc");
        Query nativeQuery = em.createNativeQuery(buffer.toString());
        nativeQuery.setParameter("userId",userId==null?0:userId);
        nativeQuery.setParameter("teamId",temaId);
        nativeQuery.setParameter("mUserId",userId==null?0:userId);
        nativeQuery.unwrap(org.hibernate.SQLQuery.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        Map<String,Object> maps= (Map<String, Object>) nativeQuery.getSingleResult();
        maps.put("members",teamMemberRepository.findByTeamIdAndStatusOrderByApplyTimeDesc(temaId,1));
        maps.put("applyMembers",teamMemberRepository.findByTeamIdAndStatusOrderByApplyTimeDesc(temaId,0));
        return maps;

    }
}
